﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;


namespace Volunteer
{
    public partial class _Default : System.Web.UI.Page
    {
        private string ConnectionString = ConfigurationManager.ConnectionStrings["VolunteerDBConnectionString"].ConnectionString;
        private DBConn dbConn = new DBConn();

        protected void Page_Load(object sender, EventArgs e)
        {
            //Assign currently logged in user to Entered By textbox
            txtEnteredBy.Text = Globals.UserName;
        }
               

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                //get all values needed to upload volunteer into db
                VolunteerInfo v = new VolunteerInfo();
                bool bSuccess = false;

                v.FirstName = txtFName.Text;
                v.LastName = txtLName.Text;
                v.Street = txtAddress.Text;
                v.City = txtCity.Text;
                v.State = txtState.Text;

                v.Zip = Convert.ToInt32(txtZip.Text);

                v.WorkPhone = txtWPhone.Text;
                v.CellPhone = txtCPhone.Text;
                v.HomePhone = txtHPhone.Text;
                v.Fax = txtFax.Text;
                v.Email = txtEmail.Text;
                v.Title = ddlTitle.SelectedValue;
                v.Status = txtStatus.Text;
                
                v.EnteredBy = txtEnteredBy.Text;
                v.Notes = txtNotes.Text;

                v.DOB = Convert.ToDateTime(txtDOB.Text);
                v.HireDate = Convert.ToDateTime(txtHDate.Text);
                string strTerm = txtTDate.Text;

                //Term Date is allowed to be null, however, DB is not accepting null values therefore set to 1/1/1900
                if (!string.IsNullOrEmpty(strTerm))
                {
                    v.TermDate = Convert.ToDateTime(txtTDate.Text);
                }
                else
                { 
                    v.TermDate = Convert.ToDateTime("1/1/1900"); 
                }

                v.Ethnicity = txtEthnicity.Text;
                v.Gender = ddlGender.SelectedValue;
                v.AgeRange = Convert.ToInt32(ddlAgeRange.SelectedValue);
                v.County = txtCounty.Text;
                v.Disability = txtDisability.Text;
                v.EducationLevel = Convert.ToInt32(ddlEduLevel.SelectedValue);

                try
                {
                    bSuccess = dbConn.Volunteer_Add(v);
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                if (bSuccess)
                { 
                    trMessage.Visible = true;
                    lblMessage.Text = "Data successfully saved in database";
                }
                else
                {
                    trMessage.Visible = true;
                    lblMessage.Text = "Data has not been saved in database";
                }
            }
            
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            TextBox txtBox;
            foreach (BaseValidator ctr in Page.Validators)
            {
                WebControl webCtr = (WebControl)Page.FindControl(ctr.ControlToValidate);
                if (webCtr.GetType() == typeof(TextBox))
                {
                    txtBox = (TextBox)webCtr;
                    txtBox.Text = "";
                }
                if (trMessage.Visible)
                {
                    lblMessage.Text = "";
                    trMessage.Visible = false;
                }
            }
            //check txtNotes separately since it has no validation control assigned to
            if (!string.IsNullOrEmpty(txtNotes.Text))
            { txtNotes.Text = ""; }
        }

        protected void Button11_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Reports_New.aspx");
        }

        protected void btnVolView_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/VolInfoView.aspx");
        }

        protected void btnVolHours_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/VolCalendar.aspx");
        }
                       
    }
    

}
